﻿using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ConfExpert
{
    public partial class ChangePassword : XtraForm
    {
        string KullaniciAdi;
        string DatabaseNamePostFix = "";
        public ChangePassword(string KullaniciAdi, string DatabaseNamePostFix)
        {
            InitializeComponent();
            this.KullaniciAdi = KullaniciAdi;
            this.DatabaseNamePostFix = DatabaseNamePostFix;
        }

        private void ChangePassword_Load(object sender, EventArgs e)
        {
            btnChange.Click += btnChange_Click;
        }

        void btnChange_Click(object sender, EventArgs e)
        {
            if (txtNewPassword.Text == txtreNewPassword.Text)
            {
                if (txtNewPassword.Text == "")
                {
                    XtraMessageBox.Show("Yeni şifre boşluk olamaz. Tekrar deneyin.");
                    return;
                }
                else if (txtNewPassword.Text.Length < 3)
                {
                    XtraMessageBox.Show("Yeni şifre 3 karakterden kısa olamaz. Tekrar deneyin.");
                    return;
                }

                if (CheckCredentials(KullaniciAdi, txtOldPassword.Text))
                {
                    string[] PasswordHashed = CryptoManager.GetSaltedHash(txtNewPassword.Text);
                    Session.DataManager.ExecuteNonQuery("UpdUserInfo", KullaniciAdi, PasswordHashed[0], PasswordHashed[1]);

                    XtraMessageBox.Show("Şifreniz başarıyla değiştirildi.");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    XtraMessageBox.Show("Kullanıcı adı ve/veya eski şifre uyuşmuyor.");

            }
            else
                XtraMessageBox.Show("Şifreler aynı değil. Tekrar deneyin.");
        }


        public bool CheckCredentials(string Username, string Password)
        {
            Session.ServerAddress = "confexpert.karbel.com";
            ceGate.ceGateSoapClient gate = new ceGate.ceGateSoapClient();
            gate.Endpoint.Address = new System.ServiceModel.EndpointAddress(string.Format("http://{0}/ceGate/ceGate.asmx", Session.ServerAddress));

            SqlConnectionStringBuilder cnnStrBuilder = new SqlConnectionStringBuilder();
            DataSet dsCredentials = null;
            try
            {
                dsCredentials = gate.GetCredentials(Username, Password, Session.ServerAddress, DatabaseNamePostFix);
            }
            catch (Exception ex)
            {
                LogManager.LogToDB(ex);
                //XtraMessageBox.Show("Sistem Hatası ! [ConfExpert Gate]:" + ex.ToString());
                return false;
            }

            if (dsCredentials == null)
                return false;

            string connectionString = dsCredentials.Tables["ConnectionString"].Rows[0]["ConnectionString"].ToString();
            cnnStrBuilder.ConnectionString = connectionString;

            ConfExpert.Datasets.CustomConnectionString.SetConnection(cnnStrBuilder.ConnectionString);
            Session.DataManager = new DataManager(cnnStrBuilder.ConnectionString);

            return true;
        }


    }
}
